/*
 * Copyright 2010 WithOne Co. Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 *     
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


package jp.co.withone.osgi.gadget.bundlecontroller.viewer.impl;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import jp.co.withone.osgi.gadget.bundlecontroller.manager.ViewManager;
import jp.co.withone.osgi.gadget.bundlecontroller.viewer.SettingViewListener;

import org.osgi.framework.Bundle;
import org.osgi.framework.BundleException;
import org.osgi.framework.ServiceReference;
import org.osgi.service.packageadmin.PackageAdmin;

/**
 * @author takada
 *
 */
public class BundleControlMouseAdapter extends MouseAdapter{

    private ViewManager manager = null;
    
    private Bundle bundle = null;
    private BundleActionCommand actionCommand = null;
    
    public BundleControlMouseAdapter(ViewManager manager){
        this.manager = manager;
    }
    
    public BundleControlMouseAdapter(ViewManager manager, Bundle bundle,BundleActionCommand actionCommand){
        this.manager = manager;
        this.bundle = bundle;
        this.actionCommand = actionCommand;
    }
    @Override
    public void mouseReleased(MouseEvent e) {
        switch (actionCommand) {
        case INSTALL:
            InstallView installView = manager.getInstallView();
            installView.setBounds(e.getXOnScreen() - installView.getWidth(), e.getYOnScreen() - installView.getHeight(), installView.getWidth(), installView.getHeight());
            installView.setVisible(true);
            break;

        case START:
            if (bundle != null) {
                try {
                    bundle.start();
                    manager.displayBundleControledMessage(actionCommand,bundle);
                } catch (BundleException e1) {
                    e1.printStackTrace();
                }
            }
            break;
        case STOP:
            if (bundle != null) {
                try {
                    bundle.stop();
                    manager.displayBundleControledMessage(actionCommand,bundle);
                } catch (BundleException e1) {
                    e1.printStackTrace();
                }
            }
            break;
        case UNINSTALL:
            if (bundle != null) {
                try {
                    bundle.uninstall();
                    manager.displayBundleControledMessage(actionCommand,bundle);
                } catch (BundleException e1) {
                    e1.printStackTrace();
                }
            }
            break;
        case UPDATE:
            if (bundle != null) {
                try {
                    bundle.update();
                    manager.displayBundleControledMessage(actionCommand,bundle);
                } catch (BundleException e1) {
                    e1.printStackTrace();
                }
            }
            break;
        case REFRESH:
            ServiceReference reference = manager.getBundleContext().getServiceReference(PackageAdmin.class.getName());
            PackageAdmin packageAdmin = (PackageAdmin)manager.getBundleContext().getService(reference);
            if (bundle == null) {
                packageAdmin.refreshPackages(manager.getBundleContext().getBundles());
            } else {
                packageAdmin.refreshPackages(new Bundle[]{bundle});
            }
            manager.displayBundleControledMessage(actionCommand,bundle);
            break;
        case SETTING:
            if (bundle != null){
                SettingViewListener listener = manager.getSettingViewListener(bundle);
                if (listener != null) {
                    listener.show(e.getXOnScreen(),e.getYOnScreen());
                }
            }
            break;
        
        default:
            break;
        }
    }
    

}
